//-----------------------------------------------------------------------
// <copyright file="ArticleDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class ArticleDAOBase
    {
        #region Common methods
        public virtual Article CreateArticleFromReader(IDataReader reader)
        {
            Article item = new Article();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ArticleId"))) item.ArticleId = (long)reader["ArticleId"];
                if (!reader.IsDBNull(reader.GetOrdinal("SubCatId"))) item.SubCatId = (long)reader["SubCatId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) item.Summary = (string)reader["Summary"];
                if (!reader.IsDBNull(reader.GetOrdinal("Content"))) item.Content = (string)reader["Content"];
                if (!reader.IsDBNull(reader.GetOrdinal("ImageThumnail"))) item.ImageThumnail = (string)reader["ImageThumnail"];
                if (!reader.IsDBNull(reader.GetOrdinal("Image"))) item.Image = (string)reader["Image"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) item.CreatedBy = (long)reader["CreatedBy"];
                if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) item.UpdatedDate = (DateTime)reader["UpdatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("UpdatedBy"))) item.UpdatedBy = (long)reader["UpdatedBy"];
                if (!reader.IsDBNull(reader.GetOrdinal("Approved"))) item.Approved = (bool)reader["Approved"];
                if (!reader.IsDBNull(reader.GetOrdinal("Status"))) item.Status = (long)reader["Status"];
                if (!reader.IsDBNull(reader.GetOrdinal("ParrentId"))) item.ParrentId = (long)reader["ParrentId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Version"))) item.Version = (long)reader["Version"];
                if (!reader.IsDBNull(reader.GetOrdinal("HotNews"))) item.HotNews = (bool)reader["HotNews"];
                if (!reader.IsDBNull(reader.GetOrdinal("TitleEn"))) item.TitleEn = (string)reader["TitleEn"];
                if (!reader.IsDBNull(reader.GetOrdinal("SummaryEn"))) item.SummaryEn = (string)reader["SummaryEn"];
                if (!reader.IsDBNull(reader.GetOrdinal("ContentEn"))) item.ContentEn = (string)reader["ContentEn"];
                if (!reader.IsDBNull(reader.GetOrdinal("Vietnam"))) item.Vietnam = (bool)reader["Vietnam"];
                if (!reader.IsDBNull(reader.GetOrdinal("English"))) item.English = (bool)reader["English"];
                if (!reader.IsDBNull(reader.GetOrdinal("Price"))) item.Price = (string)reader["Price"];
                if (!reader.IsDBNull(reader.GetOrdinal("Size"))) item.Size = (string)reader["Size"];
                if (!reader.IsDBNull(reader.GetOrdinal("NewPro"))) item.NewPro = (bool)reader["NewPro"];
                if (!reader.IsDBNull(reader.GetOrdinal("CodePro"))) item.CodePro = (string)reader["CodePro"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateArticleFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateArticle methods
            
        public virtual void CreateArticle(Article article)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleInsert");
                
                database.AddInParameter(dbCommand, "@SubCatId", DbType.Int64, article.SubCatId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, article.Title);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, article.Summary);
                database.AddInParameter(dbCommand, "@Content", DbType.String, article.Content);
                database.AddInParameter(dbCommand, "@ImageThumnail", DbType.String, article.ImageThumnail);
                database.AddInParameter(dbCommand, "@Image", DbType.String, article.Image);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, article.Enable);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, article.Order);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, article.CreatedDate);
                database.AddInParameter(dbCommand, "@CreatedBy", DbType.Int64, article.CreatedBy);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, article.UpdatedDate);
                database.AddInParameter(dbCommand, "@UpdatedBy", DbType.Int64, article.UpdatedBy);
                database.AddInParameter(dbCommand, "@Approved", DbType.Boolean, article.Approved);
                database.AddInParameter(dbCommand, "@Status", DbType.Int64, article.Status);
                database.AddInParameter(dbCommand, "@ParrentId", DbType.Int64, article.ParrentId);
                database.AddInParameter(dbCommand, "@Version", DbType.Int64, article.Version);
                database.AddInParameter(dbCommand, "@HotNews", DbType.Boolean, article.HotNews);
                database.AddInParameter(dbCommand, "@TitleEn", DbType.String, article.TitleEn);
                database.AddInParameter(dbCommand, "@SummaryEn", DbType.String, article.SummaryEn);
                database.AddInParameter(dbCommand, "@ContentEn", DbType.String, article.ContentEn);
                database.AddInParameter(dbCommand, "@Vietnam", DbType.Boolean, article.Vietnam);
                database.AddInParameter(dbCommand, "@English", DbType.Boolean, article.English);
                database.AddInParameter(dbCommand, "@Price", DbType.String, article.Price);
                database.AddInParameter(dbCommand, "@Size", DbType.String, article.Size);
                database.AddInParameter(dbCommand, "@NewPro", DbType.Boolean, article.NewPro);
                database.AddInParameter(dbCommand, "@CodePro", DbType.String, article.CodePro);
                database.AddOutParameter(dbCommand, "@ArticleId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                article.ArticleId = (long)database.GetParameterValue(dbCommand, "@ArticleId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateArticleException, ex);
            }
        }

        #endregion

        #region UpdateArticle methods
        
        public virtual void UpdateArticle(Article article)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleUpdate");            
                
                database.AddInParameter(dbCommand, "@ArticleId", DbType.Int64, article.ArticleId);
                database.AddInParameter(dbCommand, "@SubCatId", DbType.Int64, article.SubCatId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, article.Title);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, article.Summary);
                database.AddInParameter(dbCommand, "@Content", DbType.String, article.Content);
                database.AddInParameter(dbCommand, "@ImageThumnail", DbType.String, article.ImageThumnail);
                database.AddInParameter(dbCommand, "@Image", DbType.String, article.Image);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, article.Enable);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, article.Order);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, article.CreatedDate);
                database.AddInParameter(dbCommand, "@CreatedBy", DbType.Int64, article.CreatedBy);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, article.UpdatedDate);
                database.AddInParameter(dbCommand, "@UpdatedBy", DbType.Int64, article.UpdatedBy);
                database.AddInParameter(dbCommand, "@Approved", DbType.Boolean, article.Approved);
                database.AddInParameter(dbCommand, "@Status", DbType.Int64, article.Status);
                database.AddInParameter(dbCommand, "@ParrentId", DbType.Int64, article.ParrentId);
                database.AddInParameter(dbCommand, "@Version", DbType.Int64, article.Version);
                database.AddInParameter(dbCommand, "@HotNews", DbType.Boolean, article.HotNews);
                database.AddInParameter(dbCommand, "@TitleEn", DbType.String, article.TitleEn);
                database.AddInParameter(dbCommand, "@SummaryEn", DbType.String, article.SummaryEn);
                database.AddInParameter(dbCommand, "@ContentEn", DbType.String, article.ContentEn);
                database.AddInParameter(dbCommand, "@Vietnam", DbType.Boolean, article.Vietnam);
                database.AddInParameter(dbCommand, "@English", DbType.Boolean, article.English);
                database.AddInParameter(dbCommand, "@Price", DbType.String, article.Price);
                database.AddInParameter(dbCommand, "@Size", DbType.String, article.Size);
                database.AddInParameter(dbCommand, "@NewPro", DbType.Boolean, article.NewPro);
                database.AddInParameter(dbCommand, "@CodePro", DbType.String, article.CodePro);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateArticleException, ex);
            }
        }
        
        #endregion

        #region DeleteArticle methods
        public virtual void DeleteArticle(long articleId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleDelete");
                
                database.AddInParameter(dbCommand, "@ArticleId", DbType.Int64, articleId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteArticleException, ex);
            }
        }

        #endregion

        #region GetArticle methods
        
        public virtual Article GetArticle(long articleId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleGet");
                
                database.AddInParameter(dbCommand, "@ArticleId", DbType.Int64, articleId);
                
                Article article =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        article = CreateArticleFromReader(reader);
                        reader.Close();
                    }
                }
                return article;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetArticleException, ex);
            }
        }
        
        #endregion

        #region GetArticleList methods
        public virtual ArticleCollection GetArticleList(ArticleColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ArticleCollection articleCollection = new ArticleCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Article article = CreateArticleFromReader(reader);
                        articleCollection.Add(article);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return articleCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetArticleListException, ex);
            }
        }
        
        public virtual ArticleCollection GetArticleList(ArticleColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetArticleList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetArticleList methods
        public virtual ArticleCollection GetArticleListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spArticleGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ArticleCollection articleCollection = new ArticleCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Article article = CreateArticleFromReader(reader);
                        articleCollection.Add(article);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return articleCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetArticleListException, ex);
            }
        }
        
        public virtual ArticleCollection GetArticleListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetArticleListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}